<?php
namespace api\modules\v1\controllers;

use Yii;
use api\components\ApiController;
use api\modules\v1\models\Broker;

/**
 * 经纪人控制器
 * @author CGA
 */
class BrokerController extends ApiController
{
    /**
     * 申请成为经纪人
     * @author CGA
     */
    public function actionApply()
    {
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_APPLY);
        if ($result = $model->applyToBroker()) {
            return ['message' => '恭喜成为经纪人'];
        }
    }

    /**
     * 查询经纪人额度
     */
    public function actionQueryAmount()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_QUERY_AMOUNT);
        $model->load($params, '');
        if ($result = $model->queryAmount()) {
            return ['message' => '查询成功', 'data' => $result];
        }
    }

    /**
     * 设置担保额
     */
    public function actionSetamount()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_SETAMOUNT);
        $model->load($params, '');
        if ($result = $model->setAmount()) {
            return ['message' => '设置成功', 'data' => $result];
        }
    }

    /**
     * 获取经纪人列表
     */
    public function actionListBrokers()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_LIST);
        $model->load($params, '');
        if ($result = $model->getLists()) {
            return ['message' => '获取成功', 'data' => $result];
        }
    }

    /**
     * 经纪人确认收款
     */
    public function actionReceiveMoney()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_RECEIVE_MONEY);
        $model->load($params, '');
        if ($result = $model->receiveMoney()) {
            return ['message' => '确认收款成功'];
        }
    }

    /**
     * 经纪人确认已转款
     */
    public function actionRemittance()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_REMITTANCE);
        $model->load($params, '');
        if ($result = $model->remittance()) {
            return ['message' => '转款成功'];
        }
    }

    /**
     * 获取信任的经纪人列表
     */
    public function actionListTrusts()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_TRUST_LIST);
        $model->load($params, '');
        if ($result = $model->getTrustLists()) {
            return ['message' => '获取成功', 'data' => $result];
        }
    }

    /**
     * 添加信任经纪人
     */
    public function actionAddTrust()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_TRUST_ADD);
        $model->load($params, '');
        if ($result = $model->addTrustBroker()) {
            return ['message' => '添加成功', 'data' => $result];
        }
    }

    /**
     * 删除信任经纪人
     */
    public function actionDeleteTrust()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_TRUST_DELETE);
        $model->load($params, '');
        if ($result = $model->deleteTrustBroker()) {
            return ['message' => '删除成功', 'data' => $result];
        }
    }

    /**
     * 修改信任经纪人
     */
    public function actionUpdateTrust()
    {
        $params = Yii::$app->request->post();
        $model = new Broker();
        $model->setScenario(Broker::SCENARIO_TRUST_UPDATE);
        $model->load($params, '');
        if ($result = $model->updateTrustBroker()) {
            return ['message' => '更新成功', 'data' => $result];
        }
    }
}
